// 路由守卫页面
import { useEffect } from 'react'
import { useRoutes, useLocation, useNavigate } from 'react-router-dom'
import router from './index'
import { message } from 'antd'
/**
 * 
 * @returns 首页
 */
function ToLogin() {
    const navigateTo = useNavigate()
    useEffect(() => {
        navigateTo("/login")
    },[])
    return <div></div>
}
/**
 * 
 * @returns 首页
 */
function ToPage1() {
    const navigateTo = useNavigate()
    useEffect(() => {
        navigateTo("/page1")
    },[])
    return <div></div>
}

export function BeforeRouterEnter() {
    const outlet = useRoutes(router)
    const token = localStorage.getItem("token")
    const location = useLocation()
    if (token) {
        if (location.pathname === '/login') {
            message.warning("您已登录，无须返回登录页")
            return <ToPage1 />
        }else {
            return outlet
        }
    } else {
        if (location.pathname !== '/login') {
            message.warning("请先登录")
            return <ToLogin />
        } else {
            return outlet
        }
    }
}